﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class GEditorLoader : IGAssetLoader{

    private Dictionary<string, string> resInfoDic = new Dictionary<string, string>();

    private Dictionary<string, Object> resDic = new Dictionary<string, Object>();

    public GEditorLoader()
    {
        Init();
    }

    public void Init()
    {
        string path = Application.dataPath + "/GResources";
        resInfoDic = IO.GetFilesDic(path);
    }

    public T GetRes<T>(string path) where T : Object
    {
         T obj = null;
        if(!resDic.ContainsKey(path))
        {
            if(resInfoDic.ContainsKey(path))
            {
                obj = AssetDatabase.LoadAssetAtPath<T>(resInfoDic[path]);
                resDic.Add(path, obj);
            }
            else
            {
                Debug.LogWarning("资源:" + path + "不存在!!!!!!!!!!");
            }
        }
        else
        {
            obj = resDic[path] as T;
        }
        return obj;
    }

    public Object GetRes(string path)
    {
        Object obj = null;
        if (!resDic.ContainsKey(path))
        {
            if (resInfoDic.ContainsKey(path))
            {
                obj = AssetDatabase.LoadAssetAtPath<Object>(resInfoDic[path]);//AssetDatabase.LoadAssetAtPath(resInfoDic[path]);
                resDic.Add(path, obj);
            }
            else
            {
                Debug.LogWarning("资源:" + path + "不存在!!!!!!!!!!");
            }
        }
        else
        {
            obj = resDic[path];
        }
        return obj;
    }

    public void GetResAsync<T>(string path, System.Action<string, T> callback) where T : Object
    {
        T obj = null;
        if(!resDic.ContainsKey(path))
        {
            if(resInfoDic.ContainsKey(path))
            {
                obj = AssetDatabase.LoadAssetAtPath<T>(resInfoDic[path]);
                resDic.Add(path, obj);
            }
            else
            {
                Debug.LogWarning("资源:" + path + "不存在!!!!!!!!!!");
            }
        }
        else
        {
            obj = resDic[path] as T;
        }
        callback(path, obj);
    }

    public void GetResAsync(string path, System.Action<string, Object> callback)
    {
        Object obj = null;
        if (!resDic.ContainsKey(path))
        {
            if (resInfoDic.ContainsKey(path))
            {
                obj = AssetDatabase.LoadAssetAtPath<Object>(resInfoDic[path]);
                resDic.Add(path, obj);
            }
            else
            {
                Debug.LogWarning("资源:" + path + "不存在!!!!!!!!!!");
            }
        }
        else
        {
            obj = resDic[path];
        }
        callback(path, obj);
    }

    public void UnLoad(string path, bool unloadImmediately = false)
    {

    }
}
#endif